$(function(){

	$('.calendar_content_file3 >ul >li:even').addClass('bcolor');
	$('.calendar_content_file4 >ul >li:even').addClass('bcolor');
	$('#myDialog_box2').hide();

	$('.richeng_dialog').dialog({
		autoOpen:false,
		title:'新建日程',
		modal:true,
		resizable:false,
		width:500,
		height:540,
		buttons:{
			'提交':function(){
					
				var kX = $("#businessAttribute2").val().split(' ');
				var k1 = kX[0];
				var k2 = kX[1];
				$('#businessAttribute2').val(k1);
				$('#businessAttribute3').val(k2);
				
				kY = $("#businessAttribute4").val().split(' ');
				k1 = kY[0];
				k2 = kY[1];
				$('#businessAttribute4').val(k1);
				$('#businessAttribute6').val(k2);
			
				var date1=$('#businessAttribute2').val()+','+$('#businessAttribute3').val() 
				
				var date2=$('#businessAttribute4').val()+','+$('#businessAttribute6').val() 
				if(kX!=date1||kY!=date2){
					zfalert("提示","请重新填写时间！");
					$('#businessAttribute2').val('');
					$('#businessAttribute3').val('');
					$('#businessAttribute4').val('');
					$('#businessAttribute6').val('');
					return false;
				}
				
			   var url=getRootPath()+"/generalservice/insertCalendar";
			   var successUrl=getRootPath()+"/generalservice/showForm?nextFreemarkId=4001201702150000001&businessTableName=BUS_CALENDAR&methodName=readCalendarByDay";
				submitInsertAjax(url,$(insertForm).serialize(),successUrl);
				
				},
			}
		});
		
	$('.richeng_check_dialog').dialog({
		autoOpen:false,
		title:'修改日程',
		modal:true,
		resizable:false,
		width:500,
		height:540,	
		buttons:{
			'提交':function(){
			
				var k =  updateForm.businessAttribute2.value.split(' ');
				var k1 = k[0];
				var k2 = k[1];
				updateForm.businessAttribute2.value=k1;
				updateForm.businessAttribute3.value=k2;
				
				k = updateForm.businessAttribute4.value.split(' ');
				k1 = k[0];
				k2 = k[1];
				updateForm.businessAttribute4.value=k1;
				updateForm.businessAttribute6.value=k2;
				
			   var url=getRootPath()+"/generalservice/update";
			   var successUrl=getRootPath()+"/generalservice/showForm?nextFreemarkId=4001201702150000001&businessTableName=BUS_CALENDAR&methodName=readCalendarByDay";
			   submitUpdateAjax(url,$(updateForm).serialize(),successUrl);
				
				},
			}
		});
		
		//流程任务详情弹框
		$('.myDialog').dialog({
		autoOpen:false,
		title:'标题',
		modal:true,
		resizable:false,
		width:1000,
		height:570,
		buttons:{
			}
		});
		$('.aClick').css('background','#E8E5E5');
		$('.aClick').click(function(){
			$('#myDialog_box2').hide();
			$('#myDialog_box1').show();
			$('.aClick').css('background','#E8E5E5');
			$('.bClick').css('background','#F6F6F6');
		});
		/*$('.bClick').click(function(){
			$('#myDialog_box1').hide();
			$('#myDialog_box2').show();
			$('.bClick').css('background','#E8E5E5');
			$('.aClick').css('background','#F6F6F6');
		});*/
		$('.fujian_dialog').dialog({
		autoOpen:false,
		title:'附件列表',
		modal:true,
		resizable:false,
		width:400,
		height:300,
		buttons:{
			'下载':function(){

				},
			}
		});
		 
		$('.changYY_dialog').click(function(){
	            $('.changyongyu_dialog').dialog('open');
	        });
		 
		//常用语弹框
        $('#changyongyu_dialog').dialog({
            autoOpen:false,
            title:'常用语',
            modal:true,
            resizable:false,
            width:400,
            height:300,
            buttons:{
                '确定':function(){
                        var ckvName =''; 
                        $('input[name="changyongyu"]:checked').each(function(){ 
                            if(ckvName==''){
                                ckvName = $(this).parents('ul').find('li:last').find('a').text();
                            }else{
                                ckvName += '\r\n'+$(this).parents('ul').find('li:last').find('a').text(); 
                            }
                        }); 
                        $("#auditOpinion").val(ckvName);
                        $('#changyongyu_dialog').dialog('close');
                    },
                }
        });
        
        $('.fujian_dialog').dialog({
            autoOpen:false,
            title:'附件列表',
            modal:true,
            resizable:false,
            width:400,
            height:300,
            buttons:{
                '关闭':function(){
                    $('.fujian_dialog').dialog('close');
                    },
                }
          });
});	

$('.clickNewCalendar').click(function(){
		 $('#id').val("");
		 $('#businessAttribute13').val("");
         $('#businessAttribute1').val("");
         $('#businessAttribute2').val("");
         $('#businessAttribute4').val("");
         $('#businessAttribute10').val("");
         $('#businessAttribute5').val("");
         $('#businessAttribute7').val("");
         $('#businessAttribute8').val("");
         $('#businessAttribute9').val("");
		 $('.richeng_dialog').dialog({
		 		title:'新建日程',	
		 	});
		$('.richeng_dialog').dialog('open');
	});
	

	
	
		
//后加
$('#calendar_content_file1 .calendar_content_file_box >li >div').click(function(){
		$(this).css('background', 'url(../images/u540.png)').css('color', '#FFFFFF');
		$(this).parent().siblings().find('div').css('background', '#F2F2F2').css('color', '#6CD26C');
		$(this).parent().parent().siblings().find('div').css('background', '#F2F2F2').css('color', '#6CD26C');
		$('.time').css('background', '#F2F2F2').css('color', '#6CD26C');
	});
	
$('.calendar_content_file_box >ul >li div').click(function(){
		$(this).css('background', 'url(../images/u540.png)').css('color', '#FFFFFF');
		$(this).parent().siblings().find('div').css('background', '#F2F2F2').css('color', '#6CD26C');
		$(this).parent().parent().siblings().find('div').css('background', '#F2F2F2').css('color', '#6CD26C');
		$('.time').css('background', '#F2F2F2').css('color', '#6CD26C');
	});

$('#calendar_content_file2 .calendar_content_file_box >ul >li div').click(function(){
		$(this).css('background', 'url(../images/u550.png)').css('color', '#FFFFFF');
		$(this).parent().siblings().find('div').css('background', '#F2F2F2').css('color', '#6CD26C');
		$(this).parent().parent().siblings().find('div').css('background', '#F2F2F2').css('color', '#6CD26C');
		$('.time').css('background', '#F2F2F2').css('color', '#6CD26C');
	});
		
$('.notice_nav_GGTZ >div').click(function(){
		$('.notice_nav_GGTZ >div').eq(4).unbind('click');
		$(this).css('background', '#FFFFFF').css('border-bottom','none');
		$(this).siblings().css('background', '#EDF6FB').css('border-bottom', '1px solid #C1D3DE');
		$('.notice_nav_GGTZ > div').find('span').siblings().hide();
		$(this).find('span').siblings().show();
	});
		
$('.notice_content_left a').click(function(){
		$(this).parent().next().slideToggle('1000');
	});

	
$('.fujianXiazai').click(function(){
        $('.fujian_dialog').dialog('open');
    });
        
        
//查看个人详情列表(generalservice方法)
function contentClick(id, processdefinitionId,processinstanceId,assignee){
	    handle(processdefinitionId, processinstanceId, id);
	    $('.myDialog').dialog('open');      
    };
function imgClick(id){
	$('.richeng_check_dialog').dialog('open');
    $.ajax({
            type: "POST",
            url: getRootPath()+"/generalservice/get/" +id+"/BUS_CALENDAR/"+new Date(),
            dataType: "json",
            async:false , 
            
            success: function(data,status) {
               if(data.status=="0"){
                   alert(data.message);
               }else{
                    var myDate = new Date();
                  updateForm.businessAttribute1.value=data.bean.businessAttribute1;
                  updateForm.businessAttribute2.value=data.bean.businessAttribute2 +' '+data.bean.businessAttribute3;
                  //updateForm.businessAttribute4.value=myDate.getFullYear()+'-'+myDate.getMonth()+'-'+myDate.getDate()+' '+myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
                  updateForm.businessAttribute4.value=data.bean.businessAttribute4 +' '+data.bean.businessAttribute6;
                  updateForm.businessAttribute10.value=data.bean.businessAttribute10;
                  updateForm.businessAttribute5.value=data.bean.businessAttribute5;
                  updateForm.businessAttribute13.value=data.bean.businessAttribute13;
                  updateForm.businessAttribute7.value=data.bean.businessAttribute7;
                  updateForm.businessAttribute8.value=data.bean.businessAttribute8;
                  updateForm.businessAttribute9.value=data.bean.businessAttribute9;
                  updateForm.id.value=data.bean.id;
               }
           },
           error:function(xhr,s1,s2){
            var message = xhr.responseText;
            eval("var json = "+message);
            if($.trim(json.error).length==0){
                alert('出错');
            }else{
                alert(json.error);
            }
           }
        });
	
}
var pid='';
var pdid='';
function handle(processDefinitionId, processInstanceId, taskId){
	pid=processInstanceId;
	pdid=processDefinitionId;
    $.ajax({
        type:'post',//可选get
        url:getRootPath()+'/workprocess/getFormData?date='+new Date(),
        data:{'processDefinitionId':processDefinitionId,'taskId':taskId},
        dataType:'json',
        async:false , //false为同步
        success:function(data,status){
          if(data.status=="0"){
                alert(data.message);
           }else{
                //form表单拼接
                var formDataList = data.formDataList;
                var startFormDataList = data.startFormDataList;
                var startFormDataValueMap = data.startFormDataValueMap;
                var enumTypeMap = data.enumTypeMap;
                var formHtml = operFormDateHtml(startFormDataList, formDataList, startFormDataValueMap, enumTypeMap);
                $('#formhtml').html(formHtml);
                
                //设置审核框标题
                if(!isStrEmpty(startFormDataValueMap)){
                    $('.myDialog').dialog({title:startFormDataValueMap._DFHC_PROCESS_NAME_JBZX_});
                }
                
                //大的审核意见
                var auditInfos = data.auditInfoVos;
                var bigAuditHtml = operBigAuditHtml(auditInfos);
                $('#bigAuditInfo').html(bigAuditHtml);
                //大的审核意见条数
                $('#auditOpinionNum').html(auditInfos.length);
                
                //节点名称
                $('#taskNodeName').html(data.taskNodeName);
                
                //小的审核意见(流程图旁边)
                var minAuditHtml = operMinAuditHtml(auditInfos);
                $('#minAuditHtml').html(minAuditHtml);
                
                //审核常用语
                var commonLanguageVos = data.commonLanguageVos;
                var commonLanguageHtml = operCommonLanguage(commonLanguageVos);
                $('#commonLanguage').html(commonLanguageHtml);
                
                //暂存的审核意见
                var temporaryMap = data.temporaryMap;
                var auditOpinion = temporaryMap.auditOpinion;
                if(auditOpinion!='' && auditOpinion!='undefined'){
                    $('#auditOpinion').val(auditOpinion);
                    $('#temporaryId').val(temporaryMap.temporaryId);
                }
                
                //附件列表
                if(!isStrEmpty(data.startFormDataValueMap)){
                    var fileList = data.startFormDataValueMap.fileList;
                    var fileListHtml = '';
                    if(!isStrEmpty(fileList)){
                        fileListHtml = operFileList(fileList);
                    }
                    $('#fujiandown').html(fileListHtml);
                }
           }        
       },
       error:function(xhr,s1,s2){
            var message = xhr.responseText;
            eval("var json = "+message);
            if($.trim(json.error).length==0){
                alert('数据异常,请刷新页面重试');
            }else{
                alert(json.error);
            }
       }
    });
    
    //给隐藏任务id赋值
    $("#auditTaskId").val(taskId);
    /*var srcurl = getRootPath()+"/workflow/diagram-viewer/index.html?processDefinitionId="+processDefinitionId+"&processInstanceId="+processInstanceId;
    $('#iframesrc').attr('src',srcurl);*/
    $('.myDialog').dialog('open');
}

function bClickClick(){
	$('#myDialog_box1').hide();
	 var url = getRootPath()+"/workflow/diagram-viewer/index.html?processDefinitionId="+pdid+"&processInstanceId="+pid;

	   var imageUrl= getRootPath()+"/workflow/resource/process-instance?pid=" +pid+ "&type=image&time="+new Date();
	   var traceUrl= getRootPath()+"/workflow/process/trace?pid=" +pid+"&time="+new Date();

	   
	var options={
			'pid':pid,
			'pdid':pdid,
			'url':url,
			'imageUrl':imageUrl,
			'traceUrl':traceUrl
			
	
	};
	
	$("#shi_process").html('<div id="imgDialog"><iframe src="' + url + '" width="100%" height="' + document.documentElement.clientHeight * 0.70 + '" /></div>');
	
	graphTrace(options);
	$('#myDialog_box2').css({"background-color":"#fff"});
	$('.bClick').css('background','#E8E5E5');
	$('.aClick').css('background','#F6F6F6');
	$('#myDialog_box2').show();
}
//拼接流程form表单数据
function operFormDateHtml(startFormDataList, formDataList, startFormDataValueMap, enumTypeMap){
	var formHtml = '';
	for(var i=0; i<startFormDataList.length; i++){
		var startFormData = startFormDataList[i];
		if(i%2==0){
  			formHtml += '<tr>';
  			formHtml += operHtml(startFormData, '1', startFormDataValueMap, enumTypeMap);//1 表示只读属性
  		}else{
  			formHtml += operHtml(startFormData, '1', startFormDataValueMap, enumTypeMap);
  			formHtml += '</tr>';
  		}
	}
	/*for(var i =0; i<formDataList.length; i++){
		var formData = formDataList[i];
		if(i%2==0){
  			formHtml += '<tr>';
  			formHtml += operHtml(formData, '0', '', '');
  		}else{
  			formHtml += operHtml(formData, '0', '', '');
  			formHtml += '</tr>';
  		}
	}*/
	return formHtml;
}

//判断数据类型,展示不同的标签
function operHtml(formData, readOnly, startFormDataValueMap, enumTypeMap){
	var html = '<td>'+formData.name+'：</td>';
	var namesign = formData.id;
	if(formData.type.name=='string'){//input标签
		if(readOnly=='1'){
			html += '<td><input type="text" readonly="readonly" value="'+((startFormDataValueMap[namesign] != undefined && startFormDataValueMap[namesign] != "")?startFormDataValueMap[namesign]:"")+'"></td>';	
		}else{
			html += '<td><input type="text" name="'+formData.id+'" value="'+formData.value+'"></td>';			
		}
	}else if(formData.type.name=='enum'){
		if(readOnly=='1'){
			var value = startFormDataValueMap[namesign];
			var list = enumTypeMap[namesign];
			for(var i=0; i<list.length; i++){
				if(list[i].key==value){
					value = list[i].value;
				}
			}
			html += '<td><input type="text" readonly="readonly" value="'+value+'"></td>';	
		}else{
			html += '<td><select name='+formData.id+'>';
			var enumList = enumTypeMap[formData.id];
			for(var i=0; i<enumList.length; i++){
				html += '<option value="'+enumList[i].key+'">';
				html += enumList[i].value;
				html += '</option>';
			}
			html += '</select></td>';
			//html += '<td><input type="text" name="'+formData.id+'"></td>';			
		}
	}else if(formData.type.name=="textarea"){
		if(readOnly=='1'){
			html += '<td><textarea  readonly="readonly" style="margin: 0px; width: 275px; height: 114px;">'+((startFormDataValueMap[namesign] != undefined && startFormDataValueMap[namesign] != "")?startFormDataValueMap[namesign]:"")+'</textarea></td>';
		}else{
			html += '<td><textarea style="margin: 0px; width: 275px; height: 114px;" name="'+formData.id+'">'+formData.value+'</textarea></td>';
		}
	}else if(formData.type.name == 'file'){
		if(readOnly=='1'){
			html = '';
		}else{
			html += '<td>'+formData.value+'<input type="file" name="'+formData.id+'"/></td>';
		}
	}
	return html;
}

//拼接大的审核意见块
function operBigAuditHtml(bigAuditInfos){
	var html = '';
	for(var i=0; i<bigAuditInfos.length; i++){
		var bigAuditInfo = bigAuditInfos[i];
		var page = (i-i%5)/5+1;
		if(page == 1){
			html += '<ul class="ul'+page+'">';
		}else{
			html += '<ul class="ul'+page+'" style="display:none;">';
		}
		html += '<li style="width:24.7%;">'+bigAuditInfo.auditName+'&nbsp;</li>';
		html += '<li style="width:24.7%;">'+bigAuditInfo.auditState+'&nbsp;</li>';
		html += '<li style="width:24.7%;">'+bigAuditInfo.auditOpinion+'&nbsp;</li>';
		html += '<li style="width:24.7%;">'+bigAuditInfo.auditTime+'&nbsp;</li>';
		html += '</ul>';
	}
	$("#auditPageCount").html((bigAuditInfos.length-bigAuditInfos.length%5)/5+1);
	return html;
}

//拼接小的审核意见块
function operMinAuditHtml(auditInfos){
    var html = '';
    for(var i=0; i<auditInfos.length; i++){
        var minAuditInfo = auditInfos[i];
        html +='<ul>';
        html += '<li>'+(i+1)+'</li>';
        html += '<li>'+minAuditInfo.auditName+'</li>';
        html += '<li>'+minAuditInfo.auditState+'</li>';
        html += '</ul>';
    }
    return html;
}

//拼接常用语
function operCommonLanguage(commonLanguageVos){
    var html = '';
    for(var i=0; i<commonLanguageVos.length; i++){
        var commonLanguage = commonLanguageVos[i];
        html += '<ul>';
        html += '<li><input type="checkbox" name="changyongyu"></li>';
        html += '<li>'+(i+1)+'</li>';
        html += '<li><a title="'+commonLanguage.businessAttribute70+'" style="overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">'+commonLanguage.businessAttribute70+'<a/></li>';
        html += '</ul>';
    }
    return html;
}

//评价文件列表
function operFileList(fileList){
    var html = '';
    for(var i=0;i<fileList.length; i++){
        var file = fileList[i];
        var path = file.filePath;
        path = path.replace(/\\/g,'\\\\');
        var fileName = file.fileName;
        html += '<ul>';
        //html += '<li><input type="checkbox" name="fujian"></li>';
        html += '<li>'+(i+1)+'</li>';
        html += '<li style="width:90%;"><a href="javascript:;" onclick="downFile(\''+path+'\',\''+fileName+'\');">'+fileName+'</a></li>';
        html += '</ul>';
    }
    return html;
}



//存为草稿
function temporary(){
    var taskId = $("#auditTaskId").val();
    auditForm.action = getRootPath()+"/workprocess/temporary/"+taskId;
    auditForm.submit();
}
function auditPage(pageType){
	//当前页
	var pageNow = parseInt($("#auditPageNow").html());
	//总页数
	var pageCount = parseInt($("#auditPageCount").html());
	//换页后的页数
	var newpageNow = pageNow;
	//判断向后向前翻页
	if(pageType == '1'){
		newpageNow = pageNow-1;
	}else{
		newpageNow = pageNow+1;
	}
	//换页后的页数小于1，则为1
	if(newpageNow < 1){
		newpageNow = 1;
	}
	//换页后的页数大于总页数，则为总页数
	if(newpageNow > pageCount){
		newpageNow = pageCount;
	}
	//换页后的页码等于当前页，不换也，反之，则换页
	if(newpageNow != pageNow ){
		$(".ul"+pageNow).hide();
		$(".ul"+newpageNow).show();
	}
	$("#auditPageNow").html(newpageNow);
}